#!/bin/sh /etc/rc.common

START=20
STOP=90

USE_PROCD=1

config_init() {
	if [ ! -s /etc/config/network ] ;then
		rm -f /etc/config/network && /rom/etc/uci-defaults/02_network
	fi
}

init_switch() {
	setup_switch() { return 0; }

	include /lib/network
	setup_switch
}

start_service() {
	init_switch
	config_init

	procd_open_instance
	procd_set_param command /sbin/netifd
	procd_set_param respawn
	procd_set_param watch network.interface
	[ -e /proc/sys/kernel/core_pattern ] && {
		procd_set_param limits core="unlimited"
		echo '/tmp/%e.%p.%s.%t.core' > /proc/sys/kernel/core_pattern
	}
	procd_close_instance
}

set_route_apcli_static() {
	wanif=`uci get network.wan.ifname`
	wanproto=`uci get network.wan.proto`
	wangw=`uci get network.wan.gateway`
	
	if [ "$wanif"x = "apcli0"x ] || [ "$wanif"x = "apclii0"x ]; then
		if [ "$wanproto"x = "static"x ]; then
			route add default gw ${wangw}
		fi
	fi
}

stop_service() {
	/sbin/wifi down
}

service_running() {
	rmmod nf_sc
	ubus -t 30 wait_for network.interface
	/sbin/wifi reload_legacy
	sleep 3
	init_switch
	insmod /lib/modules/ralink/nf_sc.ko
	boot=`uci get system.system.boot`
	if [[ "$boot" = "0" ]]; then
		mii_mgr -s -p 0 -r 0 -v 3300
		mii_mgr -s -p 1 -r 0 -v 3300
		mii_mgr -s -p 2 -r 0 -v 3300
		mii_mgr -s -p 3 -r 0 -v 3300
	fi
	set_route_apcli_static
	uci set system.system.boot=0
	uci commit system
}

validate_atm_bridge_section()
{
	uci_validate_section network "atm-bridge" "${1}" \
		'unit:uinteger:0' \
		'vci:range(32, 65535):35' \
		'vpi:range(0, 255):8' \
		'atmdev:uinteger:0' \
		'encaps:or("llc", "vc"):llc' \
		'payload:or("bridged", "routed"):bridged'
}

validate_route_section()
{
	uci_validate_section network route "${1}" \
		'interface:string' \
		'target:cidr4' \
		'netmask:netmask4' \
		'gateway:ip4addr' \
		'metric:uinteger' \
		'mtu:uinteger' \
		'table:or(range(0,65535),string)'
}

validate_route6_section()
{
	uci_validate_section network route6 "${1}" \
		'interface:string' \
		'target:cidr6' \
		'gateway:ip6addr' \
		'metric:uinteger' \
		'mtu:uinteger' \
		'table:or(range(0,65535),string)'
}

validate_rule_section()
{
	uci_validate_section network rule "${1}" \
		'in:string' \
		'out:string' \
		'src:cidr4' \
		'dest:cidr4' \
		'tos:range(0,31)' \
		'mark:string' \
		'invert:bool' \
		'lookup:or(range(0,65535),string)' \
		'goto:range(0,65535)' \
		'action:or("prohibit", "unreachable", "blackhole", "throw")'
}

validate_rule6_section()
{
	uci_validate_section network rule6 "${1}" \
		'in:string' \
		'out:string' \
		'src:cidr6' \
		'dest:cidr6' \
		'tos:range(0,31)' \
		'mark:string' \
		'invert:bool' \
		'lookup:or(range(0,65535),string)' \
		'goto:range(0,65535)' \
		'action:or("prohibit", "unreachable", "blackhole", "throw")'
}

validate_switch_section()
{
	uci_validate_section network switch "${1}" \
		'name:string' \
		'enable:bool' \
		'enable_vlan:bool' \
		'reset:bool'
}

validate_switch_vlan()
{
	uci_validate_section network switch_vlan "${1}" \
		'device:string' \
		'vlan:uinteger' \
		'ports:list(ports)'
}

service_triggers()
{
	procd_add_reload_trigger network wireless

	procd_open_validate
	validate_atm_bridge_section
	validate_route_section
	validate_route6_section
	validate_rule_section
	validate_rule6_section
	validate_switch_section
	validate_switch_vlan
	procd_close_validate
}

restart() {
	rmmod nf_sc
#	give some time for cgi to respone uhttpd, sleep 1
	sleep 1
	ifdown -a
	sleep 1
	trap '' TERM
	stop "$@"
	start "$@"
#	insmod /lib/modules/ralink/nf_sc.ko
}

reload_service() {
#	init_switch
#	ubus call network reload
#	/sbin/wifi reload_legacy
#	mii_mgr -s -p 0 -r 0 -v 3300
#	mii_mgr -s -p 1 -r 0 -v 3300
#	mii_mgr -s -p 2 -r 0 -v 3300
#	mii_mgr -s -p 3 -r 0 -v 3300
    restart
}

shutdown() {
	rmmod nf_sc
	ifdown -a
	stop
}
